#include "Main.h"

/*! Main */
#include <stdio.h>

int main()
{

	//------------------------------------------
	//Initialise the Display Class Variables		  
	//------------------------------------------
	
	Action action = NO_ACTION;
	bool arrow_keys[4] = {false, false, false, false};
	bool shoot_key = false; 
	bool pause_key = false; 
	bool exit_key = false; 

	//Define loop and rendering booleans
	bool is_exit = false;
	bool is_redraw = true;
	bool is_game_over = false;
	
	//Member Functions
	
	//------------------------------------------
	//Setup the Display Class Variables		  
	//------------------------------------------
	boost::shared_ptr<Display> display(new Display);		// Initiate the Display Object
	
	Game game;												// Initiate the Game Object

	HighScore score_io;										//Initiate the HighScore Object
	_high_score = score_io.get();

	//------------------------------------------
	//Enter the Game loop
	//------------------------------------------
	display->startTimer();							//!< Start the timer contianed in Display
	while(!is_exit)
	{
		ALLEGRO_EVENT ev; 
		al_wait_for_event(display->event_queue(), &ev);
		
		if(is_game_over)
		{
			if (ev.type ==ALLEGRO_EVENT_KEY_DOWN)
			{
				switch(ev.keyboard.keycode)
				{
					case ALLEGRO_KEY_ESCAPE:	is_exit = true;				break;	
					case ALLEGRO_KEY_ENTER:		game = Game(); _high_score = score_io.get();
												is_game_over = false;		break;				
				}
			}
		}

		else if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			GameResult result = game.update(action);		//!< Game will handle all movements, collisions and drawing	
			
			if(result == WIN)
			{
				display->drawMessage("YOU WIN", "yellow");
				score_io.update(game.score());
				is_game_over = true;
			}
			else if (result == GAME_OVER)
			{
				display->drawMessage("YOU LOSE", "red");
				score_io.update(game.score());
				is_game_over = true;
			}
			else
			is_redraw = true;
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			is_exit = true;
		}
		else if (ev.type ==ALLEGRO_EVENT_KEY_DOWN ||ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			keyboard_event(ev, action, arrow_keys, shoot_key, pause_key, exit_key, is_exit);
		}

		if(is_redraw)// && al_is_event_queue_empty(resources._event_queue))
		{
			is_redraw = false;
			draw(display, game);
			//drawNoGraphics(display, game);
		//--------------------------------------------------------------------------------------------
		}
		
	}
}

void keyboard_event(ALLEGRO_EVENT &ev,Action &action, bool arrow_keys[4], bool &shoot_key, bool &pause_key, bool &exit_key, bool &is_exit)
{
	//------------------------------------------
	//Down Keys Events
	//------------------------------------------
	if(ev.type == ALLEGRO_EVENT_KEY_DOWN && !shoot_key)
	{
		switch(ev.keyboard.keycode)
		{
			case ALLEGRO_KEY_UP:		arrow_keys[UP] = true;		action = MOVE_UP;		break;
			case ALLEGRO_KEY_DOWN:		arrow_keys[DOWN] = true;	action = MOVE_DOWN;		break;
			case ALLEGRO_KEY_RIGHT:		arrow_keys[RIGHT] = true;	action = MOVE_RIGHT;	break;
			case ALLEGRO_KEY_LEFT:		arrow_keys[LEFT] = true;	action = MOVE_LEFT;		break;
			case ALLEGRO_KEY_SPACE:		shoot_key = true;			action = SHOOT;			break;
			case ALLEGRO_KEY_X:			shoot_key = true;			action = SHOOT;			break;
			default:					cout<<"INVALID KEY SELECTION"<<endl;
		}
	}

	//------------------------------------------
	//Up Key Events
	//------------------------------------------
	else if(ev.type == ALLEGRO_EVENT_KEY_UP)
	{
		switch(ev.keyboard.keycode)
		{
			case ALLEGRO_KEY_UP:		arrow_keys[UP] = false;		break;
			case ALLEGRO_KEY_DOWN:		arrow_keys[DOWN] = false;	break;
			case ALLEGRO_KEY_RIGHT:		arrow_keys[RIGHT] = false;	break;
			case ALLEGRO_KEY_LEFT:		arrow_keys[LEFT] = false;	break;
			case ALLEGRO_KEY_SPACE:		shoot_key = false;			break;
			case ALLEGRO_KEY_X:			shoot_key = false; 			break;
			case ALLEGRO_KEY_ESCAPE:	is_exit = true;				break;		
			default:					cout<<"INVALID KEY SELECTION"<<endl;
		}
	}																						

	//------------------------------------------
	//No Keys Pressed Update
	//------------------------------------------
	if(!arrow_keys[0]&&!arrow_keys[1]&&!arrow_keys[2]&&!arrow_keys[3]&&!shoot_key) 
	{
		action = NO_ACTION;
	}

}

void draw(boost::shared_ptr<Display> display, Game &game)
{
	//Draw the Map
	display->drawMap(game.map());
	
	//Draw the Monsters
	display->drawMonsters(game.monsters());

	//Draw Digdug
	display->drawDigdug(game.digdug());

	//Draw Projectile
	display->drawProjectile(game.projectile(), game.digdug());
	
	//Draw the scorebox
	display->drawScoreBox(game.score(), game.digdugLives(),  _high_score, game.level());
	
	//Flip the display
	display->update();

	//Clear the buffer
	display-> clearBuffer();
}

void drawNoGraphics(boost::shared_ptr<Display> display, Game &game)
{
	display->drawAsGrid(game.map().elements());
	display->update();
	display-> clearBuffer();
}